<?php
require("connection.php");

if(isset($_GET['del'])){
	$sql = sprintf("delete from flashes where id=%s",$_GET['del']);
	$conn->exec($sql);
}


if(isset($_POST['question']) and isset($_POST['answer'])){
	
	$sql = sprintf("insert into flashes (question, answer, detail) values ('%s', '%s', '%s')",
				  $_POST['question'],$_POST['answer'],$_POST['detail']);
	$conn->exec($sql);	
}

// get dataset to grab in table
$sql = "select * from flashes order by id DESC";
$select = $conn->prepare($sql);
$select->execute();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Das frage</title>
</head>
<body>
	<form method="post">
		Frage: <input name="question" type="text" style="width: 450px" > <br/>
		Richtige Antwort: <input name="answer" type="text" style="width: 400px"> <br/>
		Details<br/>
		<textarea rows="3" cols="30" name="detail" style="width: 600px"></textarea>
		<br/>
		<input type="submit" value="Schreib!"> 
	</form>
	
	<table border="1" width="700px">
		<thead>
			<tr>
				<td>id</td>
				<td>question</td>
				<td>answer</td>
				<td>delete</td>
			</tr>			
		</thead>
		<tbody>
			<?php while($r = $select->fetch()) { ?>
			<tr>
				<td><?php echo($r['id']);?></td>
				<td> <b><?php echo($r['question']);?></b> </td>
				<td> <b><?php echo($r['answer']);?></b> <br/> <?php echo($r['detail']);?> </td>
				<td> <a href="?del=<?php echo($r['id']);?>">delete</a> </td>
			</tr>		
			<?php } ?>
		</tbody>
	</table>
	
</body>
</html>
